﻿using DXApplicationTangche.UC.金蝶.dto;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.金蝶
{
    public partial class Form金蝶BOM辅料出库 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockOutDetailDTO> stockOutDetailDTOs = new List<StockOutDetailDTO>();
        private Dictionary<String, BomStockOutDTO> bomStockOutDTOs = new Dictionary<string, BomStockOutDTO>();
        public Form金蝶BOM辅料出库()
        {
            InitializeComponent();
        }
        public Form金蝶BOM辅料出库(List<String> orderIds)
        {
            InitializeComponent();
            //List<String> orderIds = new List<string>();
            //orderIds.Add("109341");
            //orderIds.Add("109340");
            List<MBOMGroupQueryFacadeDTO> mBOMGroupQueryFacadeDTOs = new List<MBOMGroupQueryFacadeDTO>();
            mBOMGroupQueryFacadeDTOs = 金蝶BOMService.getMBOMGroupQueryFacadeByOrderIds(orderIds);
            //保存DTO
            //显示DTO
            Dictionary<String, BomStockOutWithStyleDTO> bomStockOutWithStyleDTOs = new Dictionary<string, BomStockOutWithStyleDTO>();
            foreach (MBOMGroupQueryFacadeDTO mBOMGroupQueryFacadeDTO in mBOMGroupQueryFacadeDTOs)
            {
                //业务主键mBomInventoryQueryDTO.fnumber
                foreach (MBomInventoryQueryDTO mBomInventoryQueryDTO in mBOMGroupQueryFacadeDTO.mBomInventoryQueryDTOS.Values)
                {
                    //存在key，build
                    if (!mBOMGroupQueryFacadeDTO.mBomInventoryQueryDTOS.ContainsKey(mBomInventoryQueryDTO.fnumber))
                    {
                        //保存DTO
                        this.bomStockOutDTOs[mBomInventoryQueryDTO.fnumber].build(mBomInventoryQueryDTO);
                        //显示DTO
                        bomStockOutWithStyleDTOs[mBomInventoryQueryDTO.fnumber].build(mBomInventoryQueryDTO);
                    }
                    //不存在key，new
                    else
                    {
                        //保存DTO
                        this.bomStockOutDTOs.Add(mBomInventoryQueryDTO.fnumber, new BomStockOutDTO(mBOMGroupQueryFacadeDTO, mBomInventoryQueryDTO));
                        //显示DTO
                        bomStockOutWithStyleDTOs.Add(mBomInventoryQueryDTO.fnumber, new BomStockOutWithStyleDTO(mBOMGroupQueryFacadeDTO, mBomInventoryQueryDTO));
                    }
                }
            }
            this.gridControl辅料需求明细.DataSource = bomStockOutWithStyleDTOs.Values;
            this.gridView辅料需求明细.RefreshData();
            this.gridView辅料需求明细.ExpandAllGroups();
            this.gridControl物料出库汇总.DataSource = this.bomStockOutDTOs.Values;
            this.gridView物料出库汇总.RefreshData();
            this.gridView物料出库汇总.ExpandAllGroups();
        }

        private void simpleButton辅料出库_Click(object sender, EventArgs e)
        {
            KindDeeApplyReturnDTO kingDeeResultDTO保存出库单 = new KindDeeApplyReturnDTO();
            foreach (BomStockOutDTO bomStockOutDTO in this.bomStockOutDTOs.Values)
            {
                //辅料清洗
                if (!bomStockOutDTO.isFabric)
                {
                    this.stockOutDetailDTOs.Add(new StockOutDetailDTO(bomStockOutDTO));
                }
            }
            //保存出库单
            kingDeeResultDTO保存出库单 = 面料审核Service.保存辅料出库单(this.stockOutDetailDTOs);
            if (kingDeeResultDTO保存出库单.IsSuccess)
            {
                MessageBox.Show("辅料出库完成");
            }
            else
            {
                MessageBox.Show(kingDeeResultDTO保存出库单.SuccessMessages);
                return;
            }
        }
    }
}
